package com.shopping.www.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.shopping.www.model.Brand;
import com.shopping.www.model.RegisterBean;
import com.shopping.www.service.AdminService;
import com.shopping.www.service.UserService;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;

@WebServlet("/ban/*")
public class AdminServlet extends BaseServlet {
    private AdminService service1 = new AdminService();
    private UserService service2 = new UserService();

    //查询商家
    public void selectByInputBan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        username = new String(username.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        String status = request.getParameter("status");
        status = new String(status.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        RegisterBean registerBean = service1.selectByInputBan(username, status);
        response.setContentType("text/json;charset=utf-8");
        String jsonString = JSON.toJSONString(registerBean);
        response.getWriter().write(jsonString);
    }
    //封禁商家
    public void banBusiness(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        username = new String(username.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        service1.banBusiness(username);
        response.getWriter().write("success");
    }
    //查询被封禁的商家
    public void selectBusinessBans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<RegisterBean> registerBean = service1.selectBusinessBans();
        response.setContentType("text/json;charset=utf-8");
        String jsonString = JSON.toJSONString(registerBean);
        response.getWriter().write(jsonString);
    }
    //解禁商家
    public void liftBanBusiness(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("content-type", "application:json;charset=utf8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST");
        response.setHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        RegisterBean registerBean = JSON.parseObject(params, RegisterBean.class);
        service1.liftBanBusiness(registerBean.getUsername());
        response.getWriter().write("success");
    }
    //根据商家名查询此商家的商品
    public void selectForBan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ConcurrentModificationException {
        String businessName = request.getParameter("businessName");
        businessName = new String(businessName.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        List<Brand> brands = service1.selectForBan(businessName);
        List<Brand> brandList=new ArrayList<>();
        for (Brand brand : brands) {
            if(service1.selectBanBrand(brand.getId())){
//                brands.remove(brand);
                brandList.add(brand);
            }
        }
        brands.removeAll(brandList);
        String jsonString = JSON.toJSONString(brands, SerializerFeature.IgnoreErrorGetter);
        //将字符串响应到页面上
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //封禁商品
    public void banBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int _id = Integer.parseInt(id);
        service1.banBrand(_id);
    }
    //查询被封禁的商品
    public void selectBanBrands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        List<Integer> banBrandIds = service1.selectBanBrandIds();
        List<Brand> brands= service1.selectBanBrands();
        String jsonString = JSON.toJSONString(brands, SerializerFeature.IgnoreErrorGetter);
        //将字符串响应到页面上
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    //查询商品解禁申请
    public void selectBanBrandApply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String brand_id = request.getParameter("brand_id");
        int _id = Integer.parseInt(brand_id);
        String apply=service1.selectBanBrandApply(_id);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(apply);

    }
    //解禁商品（通过审核）
    public void liftBanBrand(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String _id=request.getParameter("id");
        int id = Integer.parseInt(_id);
        service1.liftBanBrand(id);
    }
}
